package com.idata.controller;

import com.idata.common.constant.Result;
import com.idata.model.dto.KGGraphExploreNodeDTO;
import com.idata.model.enums.GraphAnalysisNodeTypeEnum;
import com.idata.model.request.DisputeIdRequest;
import com.idata.model.request.FilterDisputeConditionalRequest;
import com.idata.model.request.GraphExploreComprehensiveRequest;
import com.idata.model.request.IdCardRequest;
import com.idata.model.response.GraphAnalysisSelectConditionResponse;
import com.idata.model.response.GraphExploreResponse;
import com.idata.model.utils.GraphExploreCombineUtils;
import com.idata.service.services.DisputeService;
import com.idata.service.services.GraphAnalysisService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @File GraphAnalysisController.Java
 * @Modify Time      @Author    @Version    @Description
 * ------------      -------    --------    -----------
 * 2023/10/10         yd.j      1.0         None
 */
@Slf4j
@RequestMapping("/graphAnalysis")
@RestController
public class GraphAnalysisController {

    @Resource
    private GraphAnalysisService graphAnalysisService;

    @Autowired
    private DisputeService disputeService;

    /**
     * 人事纠纷
     */
    @PostMapping("/affairs/pc")
    public Result<?> getPersonnelToCaseAtlas(@RequestBody FilterDisputeConditionalRequest request) {
        List<Object> data = graphAnalysisService.getPersonnelToDisputeAtlas(request);
        return Result.success(data);
    }

    /**
     * 人人纠纷
     */
    @PostMapping("/affairs/pp")
    public Result<?> getPersonnelToPeopleAtlas(@RequestBody FilterDisputeConditionalRequest request) {
        return Result.success(graphAnalysisService.getPersonnelToPeopleAtlas(request));
    }

    @PostMapping("/disputeInfo")
    public Result<?> getCaseInfo(@RequestBody DisputeIdRequest disputeIdRequest) {
        return Result.success(disputeService.obtainDisputeInfo(disputeIdRequest.getDisputeId()));
    }

    @PostMapping("/getSelectCondition")
    public Result<?> getSelectCondition(@RequestBody IdCardRequest idCardRequest) {
        GraphAnalysisSelectConditionResponse response = graphAnalysisService.getSelectCondition(idCardRequest.getIdCard());
        return Result.success(response);
    }

//    @PostMapping("/select")
//    public Result<?> getSelectValue(@RequestParam("idCard") String idCard, ) {
//        return Result.success(graphAnalysisService.selectDisputeIds(idCard, request));
//    }

    /**
     * 人 -> 人
     */
    @PostMapping("/exploreGraph/pp")
    public Result<GraphExploreResponse<KGGraphExploreNodeDTO>> getExploreGraphPP(@RequestBody IdCardRequest idCardRequest) {
        return Result.success(graphAnalysisService.getExploreGraphPP(idCardRequest.getIdCard()));
    }

    /**
     * 事 -> 人
     */
    @PostMapping("/exploreGraph/cp")
    public Result<GraphExploreResponse<KGGraphExploreNodeDTO>> getExploreGraphCP(@RequestBody DisputeIdRequest disputeIdRequest) {
        return Result.success(graphAnalysisService.getExploreGraphCP(disputeIdRequest.getDisputeId()));
    }

    /**
     * 人 -> 事
     */
    @PostMapping("/exploreGraph/pc")
    public Result<GraphExploreResponse<KGGraphExploreNodeDTO>> getExploreGraphPC(@RequestBody IdCardRequest idCardRequest) {
        return Result.success(graphAnalysisService.getExploreGraphPC(idCardRequest.getIdCard()));
    }

    /**
     * 综合图谱探索
     */
    @PostMapping("/exploreGraph/comprehensive")
    public Result<GraphExploreResponse<KGGraphExploreNodeDTO>> getExploreGraphComprehensive(@RequestBody GraphExploreComprehensiveRequest request) {
        GraphAnalysisNodeTypeEnum graphAnalysisNodeTypeEnum = GraphAnalysisNodeTypeEnum.fromDesc(request.getNodeType());
        if (graphAnalysisNodeTypeEnum == null) {
            throw new RuntimeException("参数异常");
        }
        GraphExploreResponse<KGGraphExploreNodeDTO> data;
        if (graphAnalysisNodeTypeEnum == GraphAnalysisNodeTypeEnum.DISPUTE) {
            // 如果传入的是纠纷
            data = graphAnalysisService.getExploreGraphCP(request.getId());
        } else {
            // 如果传入的是人
            GraphExploreResponse<KGGraphExploreNodeDTO> exploreGraphPP = graphAnalysisService.getExploreGraphPP(request.getId());
            GraphExploreResponse<KGGraphExploreNodeDTO> exploreGraphPC = graphAnalysisService.getExploreGraphPC(request.getId());
            data = GraphExploreCombineUtils.combine(exploreGraphPP, exploreGraphPC);
        }
        return Result.success(data);
    }

    /**
     * 图谱探索初始化接口-人事纠纷
     */
    @PostMapping("/exploreGraphWithCondition/pc")
    public Result<GraphExploreResponse<KGGraphExploreNodeDTO>> getExploreGraphPCWithCondition(@RequestBody FilterDisputeConditionalRequest request) {
        GraphExploreResponse<KGGraphExploreNodeDTO> data = graphAnalysisService.getExploreGraphPCWithCondition(request);
        return Result.success(data);
    }

    /**
     * 图谱探索初始化接口-人人纠纷
     */
    @PostMapping("/exploreGraphWithCondition/pp")
    public Result<GraphExploreResponse<KGGraphExploreNodeDTO>> getExploreGraphPPWithCondition(@RequestBody FilterDisputeConditionalRequest request) {
        GraphExploreResponse<KGGraphExploreNodeDTO> data = graphAnalysisService.getExploreGraphPPWithCondition(request);
        return Result.success(data);
    }

    /**
     * 图谱探索初始化接口-综合图谱
     */
    @PostMapping("/exploreGraphWithCondition/comprehensive")
    public Result<GraphExploreResponse<KGGraphExploreNodeDTO>> getExploreGraphComprehensiveWithCondition(@RequestBody FilterDisputeConditionalRequest request) {
        GraphExploreResponse<KGGraphExploreNodeDTO> ppData = graphAnalysisService.getExploreGraphPPWithCondition(request);
        GraphExploreResponse<KGGraphExploreNodeDTO> pcData = graphAnalysisService.getExploreGraphPCWithCondition(request);
        GraphExploreResponse<KGGraphExploreNodeDTO> data = GraphExploreCombineUtils.combine(ppData, pcData);
        return Result.success(data);
    }

}
